﻿using System;
using System.Collections.Generic;
using System.Data;
using OFB.BLL.Context;
using OFB.BLL.Repository;
using OFB.Domain;

namespace OFB.BLL.ViewModel
{
    public class ManageTraineeBLL : IManageTraineeBLL
    {
        //Declare TraineeContext
        private readonly TraineeContext context;
        public ManageTraineeBLL()
        {
            //New context
            context = new TraineeContext();
        }

        #region ITraineeBLL Members
        /// <summary>
        /// Filter - paging and return list
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="Status"></param>
        /// <param name="TraineeId"></param>
        /// <param name="Username"></param>
        /// <param name="FirstName"></param>
        /// <param name="LastName"></param>
        /// <param name="University"></param>
        /// <param name="searchString"></param>
        /// <returns></returns>
        public ICollection<Trainee> Filter(int index, int size,bool? status, string traineeId = null, string username = null, string firstName = null, string lastName = null, string university = null, string searchString =null)
        {
            IDataParameter rowEffectedParam = context.Trainee.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            ICollection<Trainee> trainees = context.Trainee.Init("Trainee_GetByStatus", context.Trainee.BuildParameter("index", DbType.Int32,index),
                                                                                                                    context.Trainee.BuildParameter("size", DbType.Int32, size),
                                                                                                                    context.Trainee.BuildParameter("Status", DbType.Boolean, status),
                                                                                                                    context.Trainee.BuildParameter("TraineeId", DbType.String, traineeId),
                                                                                                                    context.Trainee.BuildParameter("Username", DbType.String, username),
                                                                                                                    context.Trainee.BuildParameter("FirstName", DbType.String, firstName),
                                                                                                                    context.Trainee.BuildParameter("LastName", DbType.String, lastName),
                                                                                                                    context.Trainee.BuildParameter("University", DbType.String, university),
                                                                                                                    context.Trainee.BuildParameter("SearchString",DbType.String, searchString),
                                                                                                                    rowEffectedParam)
                                                                   .MapCollection<Trainee>()
                                                                   .GetCollection();

            return trainees;
        }
        
        /// <summary>
        /// Filter - paging and return total pages after searching
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="Status"></param>
        /// <param name="TraineeId"></param>
        /// <param name="Username"></param>
        /// <param name="FirstName"></param>
        /// <param name="LastName"></param>
        /// <param name="University"></param>
        /// <param name="searchString"></param>
        /// <returns></returns>
        public int GetTotalPageSearch(int index, int size, bool? status, string traineeId = null, string username = null, string firstName = null, string lastName = null, string university = null, string searchString = null)
        {
            IDataParameter rowEffectedParam = context.Trainee.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            ICollection<Trainee> trainees = context.Trainee.Init("Trainee_GetByStatus", context.Trainee.BuildParameter("index", DbType.Int32, index),
                                                                                                                    context.Trainee.BuildParameter("size", DbType.Int32, size),
                                                                                                                    context.Trainee.BuildParameter("Status", DbType.Boolean, status),
                                                                                                                    context.Trainee.BuildParameter("TraineeId", DbType.String, traineeId),
                                                                                                                    context.Trainee.BuildParameter("Username", DbType.String, username),
                                                                                                                    context.Trainee.BuildParameter("FirstName", DbType.String, firstName),
                                                                                                                    context.Trainee.BuildParameter("LastName", DbType.String, lastName),
                                                                                                                    context.Trainee.BuildParameter("University", DbType.String, university),
                                                                                                                    context.Trainee.BuildParameter("SearchString", DbType.String, searchString),
                                                                                                                    rowEffectedParam)
                                                                   .MapCollection<Trainee>()
                                                                   .GetCollection();

            return (int)rowEffectedParam.Value;
        }
       
        /// <summary>
        /// Get trainee by Id and return list
        /// </summary>
        /// <param name="traineeId"></param>
        /// <returns></returns>
        public ICollection<Trainee> GetByTraineeId(string traineeId)
        {
            return context.Trainee.Init("Trainee_GetByTraineeId", context.Trainee.BuildParameter("TraineeId", DbType.String, traineeId))
                                                                   .MapCollection<Trainee>()
                                                                   .GetCollection();
        }

        /// <summary>
        /// Get All Trainee
        /// </summary>
        /// <returns></returns>
        public ICollection<Trainee> FindAll()
        {
            return context.Trainee.Init("Trainee_GetAllTrainee", null).MapCollection<Trainee>().GetCollection();
        }

       /// <summary>
       /// Disable trainee by traineeId
       /// </summary>
       /// <param name="traineeId"></param>
        public void DisableTrainee(string traineeId)
        {
            context.Trainee.Execute("Trainee_Disable", context.Trainee.BuildParameter("TraineeId", DbType.String, traineeId));
        }

        /// <summary>
        /// Enable trainee by traineeId
        /// </summary>
        /// <param name="traineeId"></param>
        public void EnableTrainee(string traineeId)
        {
            context.Trainee.Execute("Trainee_Enable", context.Trainee.BuildParameter("TraineeId", DbType.String, traineeId));
        }

        /// <summary>
        /// Update trainee
        /// </summary>
        /// <param name="traineeId"></param>
        /// <param name="cardId"></param>
        /// <param name="username"></param>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="university"></param>
        /// <param name="birthDate"></param>
        /// <param name="email"></param>
        public int UpdateTrainee(string traineeId, string cardId, string username, string firstName, string lastName, string university, DateTime birthDate, string email)
        {
            IDataParameter rowEffectedParam = context.Trainee.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.Trainee.Execute("Trainee_Update", context.Trainee.BuildParameter("TraineeId", DbType.String, traineeId),
                                                                                                    context.Trainee.BuildParameter("CardId", DbType.String, cardId),
                                                                                                    context.Trainee.BuildParameter("Username", DbType.String, username),
                                                                                                    context.Trainee.BuildParameter("FirstName", DbType.String, firstName),
                                                                                                    context.Trainee.BuildParameter("LastName", DbType.String, lastName),
                                                                                                    context.Trainee.BuildParameter("University", DbType.String, university),
                                                                                                    context.Trainee.BuildParameter("BirthDate", DbType.String, birthDate),
                                                                                                    context.Trainee.BuildParameter("Email",DbType.String, email),
                                                                                                    rowEffectedParam);
            return (int)rowEffectedParam.Value;
        }

        /// <summary>
        /// Change password
        /// </summary>
        /// <param name="traineeId"></param>
        /// <param name="password"></param>
        public void ChangePassword(string traineeId, string password)
        {
            context.Trainee.Execute("Trainee_ChangePassword", context.Trainee.BuildParameter("TraineeId", DbType.String, traineeId),
                                                                                                                            context.Trainee.BuildParameter("Password", DbType.String, password));
        }


        /// <summary>
        /// Create new trainee
        /// </summary>
        /// <param name="traineeId"></param>
        /// <param name="cardId"></param>
        /// <param name="username"></param>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="university"></param>
        /// <param name="birthDate"></param>
        /// <returns></returns>
        public int Create(string traineeId, string cardId, string username, string password, string firstName, string lastName, string university, DateTime birthDate)
        {
            IDataParameter rowEffectedParam = context.Trainee.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.Trainee.Execute("Trainee_Create", context.Trainee.BuildParameter("TraineeId", DbType.String, traineeId),
                                                                                                     context.Trainee.BuildParameter("CardId", DbType.String, cardId),
                                                                                                     context.Trainee.BuildParameter("Username", DbType.String, username),
                                                                                                     context.Trainee.BuildParameter("Password", DbType.String, password),
                                                                                                     context.Trainee.BuildParameter("FirstName", DbType.String, firstName),
                                                                                                     context.Trainee.BuildParameter("LastName", DbType.String, lastName),
                                                                                                     context.Trainee.BuildParameter("University", DbType.String, university),
                                                                                                     context.Trainee.BuildParameter("BirthDate", DbType.DateTime, birthDate),
                                                                                                     rowEffectedParam);
            return (int)rowEffectedParam.Value;
        }


      

        /// <summary>
        /// Validate cardId existed or not
        /// </summary>
        /// <param name="cardId"></param>
        /// <returns></returns>
        public int ValidateCardId(string traineeId, string cardId)
        {
            IDataParameter count = context.Trainee.BuildParameter("Count", DbType.Int32, ParameterDirection.Output);
            ICollection<Trainee> trainees = context.Trainee.Init("Trainee_ValidateCardId", context.Trainee.BuildParameter("TraineeId", DbType.String, traineeId),
                                                                    context.Trainee.BuildParameter("CardId", DbType.String, cardId),
                                                                    count)
                                                                  .MapCollection<Trainee>()
                                                                  .GetCollection();
            return (int)count.Value;
        }

        /// <summary>
        /// Validate username existed or not
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public int ValidateAccount(string traineeId, string username)
        {
            IDataParameter count = context.Trainee.BuildParameter("Count", DbType.Int32, ParameterDirection.Output);
            ICollection<Trainee> trainees = context.Trainee.Init("Trainee_ValidateUsername", context.Trainee.BuildParameter("TraineeId", DbType.String, traineeId), 
                                                                context.Trainee.BuildParameter("Username", DbType.String, username),
                                                                count)
                                                                 .MapCollection<Trainee>()
                                                                 .GetCollection();
            return (int)count.Value;
        }

        /// <summary>
        /// Get trainee by page index and page size
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public ICollection<Trainee> GetByPage(int index, int size)
        {
            IDataParameter rowEffectedParam = context.Trainee.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            ICollection<Trainee> trainees = context.Trainee.Init("Trainee_Paging",context.Trainee.BuildParameter("Index", DbType.Int32, index),
                                                                                                                                             context.Trainee.BuildParameter("Size", DbType.Int32, size),
                                                                                                                                             rowEffectedParam)
                                                                  .MapCollection<Trainee>()
                                                                  .GetCollection();

            return trainees;
        }

        /// <summary>
        /// Get total page by page size
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public int GetTotalPage(int index, int size)
        {
            IDataParameter rowEffectedParam = context.Trainee.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            ICollection<Trainee> trainees = context.Trainee.Init("Trainee_Paging", context.Trainee.BuildParameter("Index", DbType.Int32, index),
                                                                                                                                             context.Trainee.BuildParameter("Size", DbType.Int32, size),
                                                                                                                                             rowEffectedParam)
                                                                  .MapCollection<Trainee>()
                                                                  .GetCollection();

            return (int)rowEffectedParam.Value;
        }

        #endregion
    }
}
